<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>BairesTech</title>


<!-- Scripts --> 
<script type="text/javascript" src="js/objs.js"></script>
<script type="text/javascript" src="js/PureMVC_JS_1_0.js"></script>
<script type="text/javascript" src="js/PureMVC_JS_FSM_0_9.js"></script>





<script type="text/javascript">
<!--

function startup()
{
	var ApplicationFacade 	= Objs.load("org.puremvc.js.demos.fsm.clock.ApplicationFacade");
	var ClockView 			= Objs.load("org.puremvc.js.demos.fsm.clock.view.components.ClockView");

	//Reference views in the application
	var application = document.getElementById('application');
	application.clockView =	new ClockView (	document.getElementById('clockPanel')	);

	var facade = ApplicationFacade.getInstance();
	facade.startup(application);
}
	

function class_org_puremvc_js_demos_fsm_clock_ApplicationFacade()
{
	Objs.register("org.puremvc.js.demos.fsm.clock.ApplicationFacade",ApplicationFacade);
	
	var IFacade 				= Objs.load("org.puremvc.js.interfaces.IFacade");
	var Facade 					= Objs.load("org.puremvc.js.patterns.facade.Facade");
	var Notification 			= Objs.load("org.puremvc.js.patterns.observer.Notification");
	
	var StartupCommand 			= Objs.load("org.puremvc.js.demos.fsm.clock.controller.StartupCommand");
	
	var StartTimerCommand 		= Objs.load("org.puremvc.js.demos.fsm.clock.controller.StartTimerCommand");
	var StopTimerCommand 		= Objs.load("org.puremvc.js.demos.fsm.clock.controller.StopTimerCommand");
	var ResetTimerCommand	 	= Objs.load("org.puremvc.js.demos.fsm.clock.controller.ResetTimerCommand");
	
	function ApplicationFacade()
	{
		Facade.apply(this,arguments);
		if(Objs.extending) return;
	}
	
	Objs.extend(ApplicationFacade,Facade);
	Objs.implement(ApplicationFacade,IFacade);
	
	var o = ApplicationFacade.prototype;
	
	ApplicationFacade.STARTUP			= "startup";
	ApplicationFacade.START_TIMER		= "start";
	ApplicationFacade.STOP_TIMER		= "stop";
	ApplicationFacade.RESET_TIMER		= "reset";
	
	
	ApplicationFacade.getInstance = function()
	{
		if( !(Facade._instance instanceof  ApplicationFacade))
		{
			Facade._instance = new ApplicationFacade();
		}
		return Facade._instance;
	}
	
	o.startup = function(app)
	{
		this.sendNotification( ApplicationFacade.STARTUP, app );    
	}
	
	o._initializeController = function() 
	{
		Facade.prototype._initializeController.call(this);
		this.registerCommand( ApplicationFacade.STARTUP, 		StartupCommand );
		this.registerCommand( ApplicationFacade.START_TIMER, 	StartTimerrCommand );
		this.registerCommand( ApplicationFacade.STOP_TIMER, 	StopTimerCommand );
		this.registerCommand( ApplicationFacade.RESET_TIMER, 	ResetTimerCommand );
	}
}
	
	
function class_org_puremvc_js_demos_fsm_clock_controller_StartupCommand()
{
	Objs.register("org.puremvc.js.demos.fsm.clock.controller.StartupCommand",StartupCommand);

	var ICommand 			= Objs.load("org.puremvc.js.interfaces.ICommand");
	var SimpleCommand 		= Objs.load("org.puremvc.js.patterns.command.SimpleCommand");
	
	var TimeProxy 			= Objs.load("org.puremvc.js.demos.fsm.clock.model.TimeProxy");
	var ClockViewMediator 	= Objs.load("org.puremvc.js.demos.fsm.clock.view.ClockViewMediator");
	
	function StartupCommand()
	{
		SimpleCommand.apply(this,arguments);
		if(Objs.extending) return;
	}
	
	Objs.extend(StartupCommand,SimpleCommand);
	Objs.implement(StartupCommand,ICommand);
	
	var o = StartupCommand.prototype;
	//
	o.execute = function( note )
	{
		this._facade.registerProxy( new TimeProxy() );
		var app = note.getBody();
		this._facade.registerMediator( new ClockViewMediator(  app.clockView  ) );
	}
}



function class_org_puremvc_js_demos_fsm_clock_view_ClockView()
{
	Objs.register("org.puremvc.js.demos.fsm.clock.view.ClockView",ClockView);

	function ClockView()
	{
		
	}
	
}


//
function class_org_puremvc_js_demos_fsm_clock_view_ClockViewMediator()
{
	Objs.register("org.puremvc.js.demos.fsm.clock.view.ClockViewMediator",ClockViewMediator);

	var EventS = Objs.load("net.tekool.events.EventS");
	var Relegate = Objs.load("net.tekool.utils.Relegate");
	var IMediator = Objs.load("org.puremvc.js.interfaces.IMediator");
	var INotification = Objs.load("org.puremvc.js.interfaces.INotification");
	var Mediator = Objs.load("org.puremvc.js.patterns.mediator.Mediator");
	var Notification = Objs.load("org.puremvc.js.patterns.observer.Notification");
	var ApplicationFacade = Objs.load("org.puremvc.js.demos.js.employeeadmin.ApplicationFacade");
	
	function ClockViewMediator( viewComponent )
	{
		Mediator.apply(this, [ClockViewMediator.NAME, viewComponent] );
		if(Objs.extending) return;
		var clockForm = this._clockForm();
		clockForm.addEventListener( UserForm.ADD, Relegate.create(this,this._onAdd) );
		clockForm.addEventListener( UserForm.UPDATE, Relegate.create(this,this._onUpdate) );
		clockForm.addEventListener( UserForm.CANCEL, Relegate.create(this,this._onCancel) );
		
		this._timeProxy = this._facade.retrieveProxy( TimeProxy.NAME );
	}
	
	//
	Objs.extend(UserFormMediator,Mediator);
	Objs.implement(UserFormMediator,IMediator);
	
	
	var o = ClockViewMediator.prototype;
	
	ClockViewMediator.NAME = 'ClockViewMediator';
	//
	o._timeProxy = null;
	//
	o._clockForm  = function()
	{
		return this._viewComponent;
	}
	//
	o.listNotificationInterests = function()
	{
		return [
			ApplicationFacade.RESET_TIMER,
			ApplicationFacade.START_TIMER,
			ApplicationFacade.STOP_TIMER
		];
	}
	//
	o.handleNotification = function( note )
	{
		var clockForm = this._clockForm();
		switch ( note.getName() )
		{
			case ApplicationFacade.START_TIMER:
				clockForm.disable("cmdStart");
				clockForm.enable("cmdStop");
			break;
			case ApplicationFacade.STOP_TIMER:
				clockForm.disable("cmdStop");
				clockForm.enable("cmdStart");
			break;
			case ApplicationFacade.RESET_TIMER:
				
			break;
		}
	}
}




//var TimeProxy 			= Objs.load("org.puremvc.js.demos.fsm.clock.model.TimeProxy");
//var ClockViewMediator 	= Objs.load("org.puremvc.js.demos.fsm.clock.view.ClockViewMediator");
	
//var StartTimerCommand 	= Objs.load("org.puremvc.js.demos.fsm.clock.controller.StartTimerCommand");
//var StopTimerCommand 		= Objs.load("org.puremvc.js.demos.fsm.clock.controller.StopTimerCommand");
//var ResetTimerCommand	 	= Objs.load("org.puremvc.js.demos.fsm.clock.controller.ResetTimerCommand");










//-->
</script>

</head>

<body>

<style>
HTML,BODY
{
	background-color:#7E949F;
	font-family:Arial,Helvetica,sans-serif;
	font-size:0.9em;
	text-align:center;
}



DIV{
	font-family:Verdana, Geneva, sans-serif;
}
H1{
	font-family:Verdana, Geneva, sans-serif;
	border-bottom: 1px solid #c0c0c0;
	text-align:left;
}


#clockPanel {
	margin-left:20%;
	width: 300px;
	marker-offset: 2px;
	padding: 6px 16px 6px 6px; 
	border: 1px solid #000;
	background-color: #fff;
}
#controlBar {
	border: 1px solid #c0c0c0;
	padding: 8px 6px 6px 2px; 
	width: 300px;
	height: 22px;
	background-color: #efefef;
}
#clock #time {
	font-family:Verdana, Geneva, sans-serif;
	font-size:30px;
}
#time {
	font-weight: bold;
}

.disabled {
	visibility:hidden;	
}
.enabled {
	visibility:visible;	
}


</style>





<h1 >Pure MVC Clock Demo</h1>
<p align="left">by Leandro Milmanda Perez.</p>

    <div id="clockPanel">
    	
        <div id="clock">Time:<br/><p align="center"><span id="time">00:00:00.0</span></p></div>
        

        <!-- User List Controls -->
        <div id="controlBar" align="right">

            <!-- Start Button -->
            <input type="button" name="cmdStart" id="cmdStart" value="Start" />

            <!-- Stop Button -->
            <input type="button" name="cmdStop" id="cmdStop" value="Stop" />
            
            <!-- Reset Button -->
            <input type="button" name="cmdReset" id="cmdReset" value="Reset" />
        </div>
        
    </div>




<script>

var ClockTimer = Objs.load("org.puremvc.js.demos.fsm.clock.helper.ClockTimer");

var clocktimer = new ClockTimer("timer1");

clocktimer.update = function(time) {
	document.getElementById("time").innerHTML = time;
}

var cmdStart = document.getElementById("cmdStart");
cmdStart.onclick = function() {
	clocktimer.start();
}

var cmdStop = document.getElementById("cmdStop");
cmdStop.onclick = function() {
	clocktimer.hold();
}

var cmdReset = document.getElementById("cmdReset");
cmdReset.onclick = function() {
	clocktimer.reset();
}
	
		

function class_org_puremvc_js_demos_fsm_clock_helper_ClockTimer()
{
	Objs.register("org.puremvc.js.demos.fsm.clock.helper.ClockTimer",ClockTimer);
	
	function ClockTimer(name)
	{
		this.time = "00:00:00.0";
		
		//
		this.elapse = 100; // this is interval - 1000 millisecond
		//
		this.hms = new String(this.time).split(":");
		this.ssxx = this.hms[2];
		this.sx = new String(this.ssxx).split(".");
		this.s  = new Number(this.sx[0]);
		this.x  = new Number(this.sx[1]);
		this.m  = new Number(this.hms[1]);
		this.h  = new Number(this.hms[0]);
		this.name = name;
		this.pause = false;
		ClockTimer.timers[name] = this;
 	}
	
	ClockTimer.timers = [];
	
	var o = ClockTimer.prototype;
	//
	o.elapse = 100; // this is interval - 1000 millisecond
	o.time = "";
	//
	o.hms = [];
	o.ssxx = "";
	o.sx = [];
	o.s  = 0;
	o.x  = 0;
	o.m  = 0;
	o.h  = 0;
	o.name = "";
	o.paused = false; 
	o.status = "pause";
	
	//
	o.update  = function(){}
	
	o.sleep = function(){
		if(!this.paused){
			window.setTimeout("ClockTimer.timers['" + this.name + "'].tick()",this.elapse);	
		} else {
			this.paused = false;
			this.status = "hold";
		}
	}
	//
	o.hold = function(){
		if(this.status == "running"){
			this.paused = true;
		}
	}
	//
	o.reset = function(){
		if(this.status != "reset"){
			this.s  = 0;
			this.x  = 0;
			this.m  = 0;
			this.h  = 0;
			this.time = "00:00:00.0";
			this.update(this.time);
			this.status = "reset";
		}
	}
	//
	o.start = function() 
	{
		if(this.status != "running")
		{
			this.tick();
		}
	}
	//
	o.tick = function ()
	{
		this.x += 1;
		if(this.x > 9) 
		{
			this.x = 0;
			this.s += 1;
			if (this.s > 59)
			{
				this.s = 0;
				this.m += 1;
				
				if (this.m > 59)
				{
					this.m = 0;
					this.h += 1;
				}
			}
		}
			
		var sx = this.x ;
		var ss = this.s < 10 ? ("0" + this.s) : this.s;
		var sm = this.m < 10 ? ("0" + this.m) : this.m;
		var sh = this.h < 10 ? ("0" + this.h) : this.h;
		//
		this.time = sh + ":" + sm + ":" + ss + "." + sx;
		//
		this.status = "running";
		//
		this.update(this.time);
		//
		this.sleep();
	
	}
}

function log(s){
	try {
		console.log(s);
	} catch(e){}
}

</script>



</body>
</html>

